<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();

define('HOME_ID', 56);

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$cat_id = MiscUtils::getParam('cat_id', NEWS_TAG_ID);
$script = MiscUtils::getParam('script', 'index');

$tags = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', NULL, 't.order', 'ASC', NULL, NULL, NULL)->d;

$cond_vals = new stdClass(); 
$cond_vals->c = 't.id = :v1';
$cond_vals->v = array(':v1' => $cat_id);
$opts = new stdClass();
$opts->select_expr = 't.id, t.parent_id, t.name';
$tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'art_tag', $cond_vals, 't.order', 'ASC', NULL, NULL, $opts)->d;

$rslt2 = new stdClass();
$rslt2->parents = array();
if (count($tmps) > 0) {
  $tmp = $tmps[0];
  $rslt2->parents[] = $tmp;
  _getParentTags($tags, $tmp, $rslt2);
  rsort($rslt2->parents);
}


$tRoot = new stdClass();
$tRoot->id = 0;
$tRoot->name = 'ROOT';
$tRoot->parent_id = -1;
_createTagRoot($tags, $tRoot);

$rslt = new stdClass();
$rslt->tags = array();
_getTags($tRoot, $cat_id, $rslt, 0);
 
$SIZE = 25;
$page = MiscUtils::getParam('p', 1);
$order = MiscUtils::getParam('o', 't.date');
$queue = MiscUtils::getParam('q', 'DESC');
$cond_vals_all = NULL;
if ($cat_id != 0) {
  $cond_vals_all = new stdClass(); 
  $cond_vals_all->c = '';
  $cond_vals_all->v = array();
  foreach ($rslt->tags as $idx => &$t) {
    if ($cond_vals_all->c != '') {
      $cond_vals_all->c .= ' OR ';
    }
    $idx2 = $idx + 1;
    $cond_vals_all->c .= "t.tag_ids LIKE :v$idx2";
    $cond_vals_all->v[":v$idx2"] = "%|$t|%";
  }
}
$opts = new stdClass();
$opts->select_expr = 'Distinct t.id, t.title, t.date';
$items = DbUtils::get($myPdo,  TABLE_NAME_PREFIX_PLUGIN . 'art_post', $cond_vals_all, $order, $queue, $page, $SIZE, $opts);

function _getTags($node, $cat_id, $rslt, $is_to_add) {
  if ($node->id == $cat_id || $is_to_add == 1) {
    $rslt->tags[] = $node->id;
  }
  foreach ($node->c as &$child) {
    _getTags($child, $cat_id, $rslt, (($is_to_add === 1) ? $is_to_add : $node->id == $cat_id));
  }
}

function _createTagRoot($arr, $node) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $tmp = $arr[$i];
    if ($tmp->parent_id == $node->id) {
      $tmp->c = array();
      $node->c[] = $tmp;
      _createTagRoot($arr, $tmp);
    }
  }
}

function _getParentTags($arr, $tmp, $rslt) {
  for ($i = 0, $il = count($arr); $i < $il; $i++) {
    $obj = $arr[$i];
    if (isset($tmp) && $tmp->parent_id == $obj->id) {
      $rslt->parents[] = $obj;
      _getParentTags($arr, $obj, $rslt);
    }
  }
}

?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
.list_content {
  margin: 0px;
  border: solid 1px #dcdcdc;
  background-color: #fefefe;
}

.list_content_td ul {
  margin: 0px;
  list-style: none;
  padding: 8px;
}

.list_content_td ul li {
  width: 100%;
  vertical-align: middle;
  background: url(images/bullet.gif) no-repeat left center;
  clear: both;
  padding: 0 0 0 8px;
  height: 28px;
  font: 15px/1.5 Verdana, Arial, Helvetica, sans-serif, "宋体";
}

.list_content_td ul li a {
  float: left;
  color: #333;
  text-decoration: none;
}

.list_content_td ul li a:hover{
  text-decoration: underline;
  color: #666;
}

.list_content_td ul li span {
  float: right;
  color: #666;
  vertical-align: bottom;
  padding-right: 8px;
  font-size: 14px;
}

.list_content_td ul .line {
  float: left;
  background-image: none;
  clear: both;
  width: 100%;
  margin: 3px 0 2px 0;
  padding: 0;
  list-style: none;
  height: 1px;
  ine-height: 1px;
  border-top: 1px dashed #ccc;
}

.list_nav {
  margin: 0 0 5px 0;
  border: solid 1px #ddd;
  background-color: #fff;
  height: 33px;
  padding-left: 10px;
}

ul.nav {
  margin: 0;
  padding: 0;
  text-align: left;
  vertical-align: middle;
  font: 12px/1.6 Verdana, Arial, Helvetica, sans-serif, "宋体";
}
ul.nav li {
  display: inline;
}
ul.nav li.gap {
  padding: 0 5px;
}
ul.nav a {
  text-decoration: none;
  color: #333;
}
ul.nav a:hover {
  text-decoration: underline;
  color: #666;
}
</style>
</head>
<body>
<table cellpadding=0 cellspacing=0 style="width: 100%">
  <tr>
    <td align="left" valign="top">
      <table width="100%" cellpadding=0 cellspacing=0 class="list_nav">
        <tr><td>
          <ul class="nav">
            <li>Your Position: &nbsp;&nbsp;</li>
            <li><a href="#" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . HOME_ID; ?>'">Home</a></li>
            <li class="gap">&#8250;</li>
            <?php foreach($rslt2->parents as $idx => &$tag) { ?>
              <li><a href="#" onclick="parent.location.href = '<?php echo 'http://' . URL . '?i=' . $page_id . '&cat_id=' . $tag->id; ?>'"><?php echo $tag->name; ?></a></li>
              <?php if ($idx < count($rslt2->parents) - 1) { ?>
              <li class="gap">&#8250;</li>
              <?php } ?>
            <?php } ?>
             <?php if (count($rslt2->parents) == 0) {?>
              <li>Undefined</li>
            <?php }?>
          </ul>
        </td></tr>
      </table>
      <table width="100%" cellpadding=0 cellspacing=0 class="list_content">
			  <tr><td class="list_content_td">
			   <ul>
			  <?php foreach ($items->d as $idx => &$item) { ?>
           <li><a href="#" title="<?php echo $item->title; ?>" onclick="parent.location.href='<?php echo 'http://' . URL . '?i=' . $page_id . "&script=index_detail&cat_id=$cat_id&art_id=$item->id"; ?>';"><?php echo MiscUtils::getShortName($item->title, 50); ?></a><span><?php echo $item->date; ?></span></li>
          <?php if ($idx == count($items->d) - 1 || ($idx != 0 && ($idx+1) % 5 == 0)) { ?>
            <li class="line"></li>
			  <?php } } ?>
        </ul>
			  </td></tr>
			  <tr><td align="left" style="padding-left: 5px;"><div id="pagination" ></div></td></tr>
			</table>
	  </td>
	  <td align="left" valign="top" style="width: 10px">
	  <td align="left" valign="top" style="width: 300px">
	    <div>
        <?php include dirname(__FILE__) . '/inc.right.php'; ?>
      </div>
    </td>
	</tr>
</table>
<div style="height: 50px"></div>
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/jquery.simplePagination.js"></script>
<link type="text/css" rel="stylesheet" href="css/simplePagination.css"/>
<script type="text/javascript">
  $(function() {
    $('#pagination').pagination({
      items: <?php echo $items->t; ?>,
      itemsOnPage: <?php echo $items->s; ?>,
      currentPage: <?php echo $items->p; ?>,
      cssStyle: 'compact-theme',
      onPageClick: function(pageNumber, event) {
//        parent.location.href = 'http://<?php echo URL . '?i=' . $page_id . '&script=' . $script . '&pg='; ?>' + pageNumber;
        parent.location.href = '<?php echo "http://" . URL . "?i=" . $page_id . '&cat_id=' . $cat_id . "&o=$items->o&q=$items->q"; ?>&p=' + pageNumber;
      }
    });
  });
</script>
</body>
</html>
